/* eslint-disable no-unused-expressions */
/* eslint-disable no-sequences */
// axios 二次封装
import axios from 'axios'
import store from '../store/index'
import JSONBig from 'json-bigint'
// import { config } from 'vue/types/umd'
const instance = axios.create({
  baseURL: 'http://ttapi.research.itcast.cn',
  // baseURL: 'http://api-toutiao-web.itheima.net',
  transformResponse: [function (data) {
    try {
      return JSONBig.parse(data)
      // parse 解析
    } catch (err) {
      return data
    }
  }]
})
// 请求拦截
// eslint-disable-next-line no-unused-expressions
instance.interceptors.request.use(config => {
  const token = store.state.tokenInfo.token
  if (token) {
    console.log('token', token)
    config.headers.Authorization = `Barer ${token}`
  }
  return config
}), err => {
  return Promise.reject(err)
}
// instance.interceptors.request.use(config => {
//   const token = store.state.tokenInfo.token
//   if (token) {
//     config.headers.Authorization = `Bearer ${token}`
//   }
//   return config
// }), err => {
//   return Promise.reject(err)
// }
// // 响应拦截
instance.interceptors.response.use(response => {
  return response
}), (err) => {
  return Promise.reject(err)
}

export default instance
